import axios from "axios"


const baseUrl = () => {
    const VUE_APP_API_TYPE = process.env.VUE_APP_API_TYPE;
    const urlObj = {
        dev: 'http://localhost:7001/',     //开发
        qafc: 'http://121.36.202.154:7001/',        //测试
        online: '',
    }
    if (process.env.NODE_ENV === 'development') {
        return urlObj.dev     //开发的时候用来切不同环境
    }
    return urlObj[VUE_APP_API_TYPE]
}

const instance = axios.create({
    baseURL: baseUrl(),
    timeout: 20000
});
instance.interceptors.request.use(config => {
    // 设置header头 通过接口权限验证
    if(!config.headers['Content-Type']){
        config.headers['Content-Type'] = 'application/json';
    }
    config.headers['Authorization'] = sessionStorage.getItem('token') || ''
    return config;
});

instance.interceptors.response.use(response => {
    return Promise.resolve(response.data)
}, error => {
    return Promise.reject(error)
});

const get = (url, params = {}, config = {}) => {
    return instance({method: 'GET', url, params, ...config })
};
const post = (url, data = {}, config = {}) => {
    return instance({method: 'POST', url, data, ...config })
};
const put = (url, data, config = {}) => {
    return instance({method: "PUT", url, data, ...config})
};
const del = (url, data, config = {}) => {
    return instance({method: "DELETE", url, data, ...config})
};

const uploadUrl_ = ()=>{
    return baseUrl()
}

export {get, post, put, del,uploadUrl_ }